package com.zhoug.zmuidemo.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.zmui.ZMUIEmptyView;
import com.zhoug.zmui.ZMUITopView;
import com.zhoug.zmuidemo.R;

public class EmptyViewActivity extends AppCompatActivity {
    private Button mBtn1;
    private ZMUIEmptyView mEmptyView;
    private LinearLayout mContent;
    private ZMUITopView mTvTopView;
    private ZMUITopView mTvTopView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty_view);
        findViews();
        ;
        mBtn1.setOnClickListener(v -> {
            refresh();
        });
        refresh();
        mTvTopView.addBackImage().setOnClickListener(v -> finish());
        mTvTopView.addLeftImageView(R.drawable.image_1, false, R.id.image1).setOnClickListener(v -> {
            Toast.makeText(this, "image1", Toast.LENGTH_SHORT).show();
        });
//        ImageView imageView = mTvTopView.addRightImageView(R.drawable.icon_share_2, R.id.image2);
//        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//        imageView.setOnClickListener(v->{
//            Toast.makeText(this, "image2", Toast.LENGTH_SHORT).show();
//        });
        mTvTopView.addRightTextView("提交", R.id.textView2).setOnClickListener(v -> {
            Toast.makeText(this, "提交", Toast.LENGTH_SHORT).show();
        });
        ImageView image3 = new ImageView(this);
        image3.setImageResource(R.drawable.icon_share_2);
        image3.setScaleType(ImageView.ScaleType.FIT_CENTER);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ResourceUtils.dpTopx(30),
                RelativeLayout.LayoutParams.MATCH_PARENT);
        mTvTopView.addRightView(image3, R.id.image3, layoutParams);


        //2
        mTvTopView2.addBackImage();
        View contentView = LayoutInflater.from(this).inflate(R.layout.top_contentview, null);
        editText = contentView.findViewById(R.id.editText1);
        mTvTopView2.addContentView(contentView);
        mTvTopView2.addRightTextView("搜索", R.id.textView1).setOnClickListener(v -> {
            Toast.makeText(this, "" + editText.getText().toString(), Toast.LENGTH_SHORT).show();
        });

    }

    EditText editText;
    private volatile int i = 0;

    private void refresh() {
        mEmptyView.show(true);
        mContent.setVisibility(View.GONE);
        new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
            if (i == 6) {
                i = 1;
            }
            runOnUiThread(this::onData);


        }).start();
    }

    private void onData() {
        switch (i) {
            case 4:
                mEmptyView.hide();
                mContent.setVisibility(View.VISIBLE);
                break;
            case 2:
                mEmptyView.show("服务器连接失败", null);
                break;
            case 3:
                mEmptyView.show("服务器连接失败", "请检查网络");
                break;
            case 1:
                mEmptyView.show("网络连接失败", "请检查网络是否开启", "重试", v -> refresh());
                break;
            case 5:
                mEmptyView.show(false);
                break;
        }
    }

    private void findViews() {
        mTvTopView = findViewById(R.id.tv_topView);
        mEmptyView = findViewById(R.id.emptyView);
        mContent = findViewById(R.id.content);
        mBtn1 = findViewById(R.id.btn1);
        mTvTopView2 = findViewById(R.id.tv_topView2);
    }
}